package com.weibo.net;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.Toast;

import com.bean.newsBang.AddUser;
import com.bean.newsBang.Register;
import com.bean.newsBang.TopicList;
import com.weibo.net.AsyncWeiboRunner.RequestListener;
import com.weibo.net.RequestToken;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;

public class XinLangWeibo implements RequestListener{
	private static final String URL_ACTIVITY_CALLBACK = "weiboandroidsdk://TimeLineActivity";
	private static final String FROM = "xweibo";
	static private String uri; 
	private String oauth_verifier;
	Weibo weibo = Weibo.getInstance();
	public void get(Activity one) throws WeiboException{
		RequestToken requestToken = weibo.getRequestToken(one, Weibo.APP_KEY, 
				Weibo.APP_SECRET, this.URL_ACTIVITY_CALLBACK);
		uri = Weibo.URL_AUTHENTICATION + "?display=wap2.0&oauth_token=" + 
				requestToken.getToken() + "&from=" + this.FROM;
		Register.setUri(uri);		
		Intent intent = new Intent();
		intent.setClass(one, Register.class);
		one.startActivity(intent);
	}
	public String[] afterAction(Activity one){
		Uri u = one.getIntent().getData();
		oauth_verifier = u.getQueryParameter("oauth_verifier");
		weibo.addOauthverifier(oauth_verifier);
		try {
			weibo.generateAccessToken(one, null);
		}catch (WeiboException e1) {
			e1.printStackTrace();
		}
		String[] at={weibo.getAccessToken().getToken(),weibo.getAccessToken().getSecret()};
		return at;
	}
	
	public String upload(Activity one, Weibo weibo, String source, String file, String status, String lon, String lat) throws WeiboException{
		WeiboParameters bundle = new WeiboParameters();
		bundle.add("source", source);
		bundle.add("pic", file);
		bundle.add("status", status);
		if(!TextUtils.isEmpty(lon)){
			bundle.add("lon", lon);
		}
		if(!TextUtils.isEmpty(lat)){
			bundle.add("lat", lat);
		}
		String rlt = "";
		String  url = Weibo.SERVER + "statuses/upload.json";
		AsyncWeiboRunner weiboRunner = new AsyncWeiboRunner(weibo);
		weiboRunner.request(one, url, bundle, Utility.HTTPMETHOD_POST, this);
		return rlt;
	}

	public String update(Activity one, Weibo weibo, String source, String status, String lon, String lat) throws MalformedURLException, IOException, WeiboException{
		WeiboParameters bundle = new WeiboParameters();
		bundle.add("source", source);
		bundle.add("status", status);
		if(!TextUtils.isEmpty(lon)){
			bundle.add("lon", lon);
		}
		if(!TextUtils.isEmpty(lat)){
			bundle.add("lat", lat);
		}
		String rlt = "";
		String url = Weibo.SERVER + "statuses/update.json";
		AsyncWeiboRunner weiboRunner = new AsyncWeiboRunner(weibo);
		weiboRunner.request(one, url, bundle, Utility.HTTPMETHOD_POST, this);
		return rlt;
	}
	public void onComplete(String response) {
		// TODO Auto-generated method stub
		
	}
	public void onIOException(IOException e) {
		// TODO Auto-generated method stub
		
	}
	public void onError(WeiboException e) {
		// TODO Auto-generated method stub
		
	}
	
	
}
